<!DOCTYPE html>
<html>
<head>
	<title>cityFormat</title>
	<meta charset="utf-8">
</head>
<body>

</body>
</html>
<script type="text/javascript" src="cn_city_code_2016.js"></script>
<script type="text/javascript" src="wx_region_2018.js"></script>
<script type="text/javascript">
	// console.log(str);
	var cin={};
	function getName0(){
		for(var k in citys){
			cin[citys[k].name]={};
			for(var k1 in citys[k].child){
				citys[k].child[k1].name=citys[k].child[k1].name=="市辖区"?citys[k].name:citys[k].child[k1].name;
				cin[citys[k].name][citys[k].child[k1].name]=[];
				for(var k2 in citys[k].child[k1].child){
	                cin[citys[k].name][citys[k].child[k1].name].push(citys[k].child[k1].child[k2]);
				}
			}
		}
	    console.log(cin);
	    document.write(JSON.stringify(cin));
	}

    // 字符获取
    let reg_beijing=/11\d{4}\D+[1-9]\d{5}|11\d{4}\D+0/ig;
    let reg_cn=/[\u4e00-\u9fa5]+/ig;
    //查找省最小11最大65
    //生成省正则
    function newReg(n){
      return eval(`/${n}\\d{4}\\D+[1-9]\\d{5}|${n}\\d{4}\\D+0/ig`);
    }
    //获取省数组
    function format1(str){
      for(let i=11;i<66;i++){
      	// console.log(newReg(i));
      	let r=str.match(newReg(i));
      	// console.log(r);
      	// formatArea(i,r);
        formatAreaWithCode(i,r);
      }
    }



    //格式化省市区数组
    let oc={};
    //纯中文
    function formatArea(pi,arr){
    	let pr="",cr=[],crl=0;
    	if(!Array.isArray(arr)){return;}
    	arr.forEach(v=>{
    		//取省
    		// console.log(v.slice(0,6),pi+"0000");
    		if(v.slice(0,6)==pi+"0000"){
	         	pr=pi+v.match(reg_cn).toString();
	         	// pr=v;
	    	    oc[pr]={};
        //取市
    		}else if(v.slice(4,6)=="00"){
    			// console.log(v);
    			cr.push(v.match(reg_cn).toString());
          // cr.push(v)
    			// console.log(pr,oc[pr]);
    			oc[pr][cr[crl]]=[];
    			crl++;
    			// console.log(cr);
    	  //取区
    		}else{
    			// console.log(cr,crl,v.charAt(3));
  				if(v.slice(4,6)!="00"){
  					oc[pr][cr[crl-1]].push(v.match(reg_cn).toString());
            // oc[pr][cr[crl-1]].push(v);
  				}
    		}
    	})
    	// console.log(oc);
    }
    //带编号
    let or=[],crl=-1;
    function formatAreaWithCode(pi,arr){
      let pr="",cr=[],crl1=-1;
      if(!Array.isArray(arr)){return;}
      arr.forEach(v=>{
        //取省
        // console.log(v.slice(0,6),pi+"0000");
        if(v.slice(0,6)==pi+"0000"){
            pr=v.match(reg_cn).toString();
            // pr=v;
            or.push({
              code:v.slice(0,6),
              name:pr,
              child:[]
            });
            crl++;
            crl1=-1;
            // console.log(crl);
            // console.log(or);
        //取市
        }else if(v.slice(4,6)=="00"){
          // console.log(v);
          // console.log(or,crl,or[crl]);
          or[crl].child.push({
            code:v.slice(0,6),
            name:v.match(reg_cn).toString(),
            child:[]
          });
          crl1++;
          // console.log(or,crl);
        //取区
        }else{
          // console.log(cr,crl,v.charAt(3));
          if(v.slice(4,6)!="00"){
            or[crl].child[crl1].child.push({
              code:v.slice(0,6),
              name:v.match(reg_cn).toString()
            })

            // console.log(or);
            return;
          }
        }
      })
      // console.log(or);
    }
    format1(str);
    // console.log(oc);
    // document.write(JSON.stringify(oc));

    console.log(or);
    document.write(JSON.stringify(or));



</script>